package com.news.web.servlet.admin;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.Review;
import com.news.service.ReviewService;
import com.news.service.impl.ReviewServiceImpl;

/**
 * Servlet implementation class AdminReviewInsertServlet
 */
@WebServlet("/AdminReviewInsertServlet")
public class AdminReviewInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 设置编码方式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 获取传入数据
		String userIdStr = request.getParameter("userId");
		String adminIdStr = request.getParameter("adminId");
		String newsIdStr = request.getParameter("newsId");
		String reviewDetails = request.getParameter("reviewDetails");
		String reviewDateStr = request.getParameter("reviewDate");
		
		// 进行空值判断
		// 检查 所有值 是否为空
		if (userIdStr == null || adminIdStr == null || newsIdStr == null || reviewDetails == null
				|| reviewDateStr == null) {
			// 处理 值为空的情况
			request.setAttribute("reviewInsertBoolean", null); // 或者设定为null
			System.out.println("空");
			request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		// 定义日期格式
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		// 进行数值转换
		int userId = 0;
		int adminId = 0;
		int newsId = 0;
		Timestamp reviewDate = null;
		try {
			// 将字符串解析为日期
			//先判断传入的是用户的还是管理员的
			if("".equals(userIdStr)) {
				java.util.Date parsedDate = dateFormat.parse(reviewDateStr);
				adminId = Integer.parseInt(adminIdStr);
				newsId = Integer.parseInt(newsIdStr);
				reviewDate = new Timestamp(parsedDate.getTime());
			}else if("".equals(adminIdStr)) {
				userId = Integer.parseInt(userIdStr);
				java.util.Date parsedDate = dateFormat.parse(reviewDateStr);
				newsId = Integer.parseInt(newsIdStr);
				reviewDate = new Timestamp(parsedDate.getTime());
			}else {
				System.err.println("管理员或者用户id错误");
			}
			
			
			
		} catch (NumberFormatException | ParseException e) {
			// 处理数值转换异常的情况
			e.printStackTrace(); // 记录日志或者向用户显示错误信息
			request.setAttribute("reviewInsertBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}
		//实例化review
		Review review = new Review(userId, adminId, newsId, reviewDetails, reviewDate);
		// 创建ReviewService实例
		ReviewService reviewService = new ReviewServiceImpl();
		// 使用添加方法
		Boolean reviewInsertBoolean = reviewService.insertReview(review);
		// 使用模糊查询，由于时间问题，需要设置为null
		review.setReviewDate(null);
		List<Review> reviewQueryLikeList = reviewService.likeSelectListReview(review);
		// 将数据存入
		request.setAttribute("reviewQueryLikeList", reviewQueryLikeList);
		// 将数据存入
		request.setAttribute("reviewInsertBoolean", reviewInsertBoolean);
		System.out.println("我");
		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminReview.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
